﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter.Correspondence
{
    /// <summary>
    /// Cross correlation filter
    /// </summary>
    public class CrossCorrelation : BaseImageFilter, ITwoSourceFilter
    {
        protected bool m_bNormalized = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="CrossCorrelation"/> class.
        /// </summary>
        public CrossCorrelation()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CrossCorrelation"/> class.
        /// </summary>
        /// <param name="_crossCorrelation">The _cross correlation.</param>
        internal CrossCorrelation(CrossCorrelation _crossCorrelation)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            return null;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Processes the specified bmpimg.
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            if (m_bNormalized)
            {
                Normalized();
            }
            else
            {
                Normal(_bitmap1, _bitmap2);
            }

            //if (totaldifference == 0 || originaldiff == 0)
            //    return 0;
            //else
            //    return (totaldifference / originaldiff);
            return _bitmap2;
        }

        /// <summary>
        /// Normalizeds this instance.
        /// </summary>
        private void Normalized()
        {
        }

        /// <summary>
        /// Normals the specified _bitmap1.
        /// </summary>
        /// <param name="_bitmap1">The _bitmap1.</param>
        /// <param name="_bitmap2">The _bitmap2.</param>
        private static void Normal(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            int nOriginalHeight = _bitmap1.Height;
            int nOriginalWidth = _bitmap1.Width;
            UnsafeBitmap original = _bitmap1;
            UnsafeBitmap stego = _bitmap2;

            double totaldifference = 0, originaldiff = 0;
            Color origpix, stegpix;
            for (int i = 0; i < nOriginalHeight; i++)
            {
                for (int j = 0; j < nOriginalWidth; j++)
                {
                    origpix = original[j, i];
                    stegpix = stego[j, i];
                    originaldiff += Math.Pow(origpix.R, 2) +
                            Math.Pow(origpix.G, 2) +
                            Math.Pow(origpix.B, 2);
                    totaldifference += ((Math.Abs(origpix.R)
                            * stegpix.R)
                            + Math.Abs(origpix.R)
                                    * stegpix.G)
                                    + Math.Abs(origpix.B)
                                            * stegpix.B;
                }
            }
        }

        #endregion

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CrossCorrelation(this);
        }
    }
}
